<div class="row">
	<div class="col-xs-12">
		<div class="box">
			<div class="box-header">
				<h3 class="box-title">日志管理</h3>
				<div class="box-tools">
					<div class="input-group input-group-sm" style="width: 350px;">
						<input type="text" name="table_search" id="searchNameId"
							class="form-control pull-right" placeholder="用户名">

						<div class="input-group-btn">
							<button type="button" class="btn btn-default btn-search">
								<i class="fa fa-search"></i>
							</button>
							<button type="button" class="btn btn-default btn-delete">删除</button>
						</div>
					</div>
				</div>
			</div>
			<!-- /.box-header -->
			<div class="box-body table-responsive no-padding">
				<table class="table table-hover">
					<thead>
						<tr>
							<th><input type="checkbox" id="checkAll">全选</th>
							<th>用户名</th>
							<th>操作</th>
							<th>请求方法</th>
							<th>请求参数</th>
							<th>IP</th>
							<th>执行时长</th>
						</tr>
					</thead>
					<tbody id="tbodyId">
						<tr>
							<td colspan="7">数据正在加载中...</td>
						</tr>
					</tbody>
				</table>
			</div>
			<div id="pageId" class="box-footer clearfix"></div>
			<!-- /.box-body -->
		</div>
		<!-- /.box -->
	</div>
</div>

<script type="text/javascript">
	//console.log(),debugger,排除法
	$(function() {
		$("#pageId").load("doPageUI", function() {
			doGetObjects();
		});
		$(".input-group-btn").on("click", ".btn-search", doQueryObjects);
		$(".input-group-btn").on("click", ".btn-delete", doDeleteObjects);
		$("#checkAll").change(doChangeTBodyCheckBoxState);
		$("#tbodyId").on("change", ".cBox", doChangeTHeadCheckBoxState)

	});

	function doChangeTHeadCheckBoxState() {
		var flag = true;
		$("#tbodyId input[type='checkbox']").each(function() {
			flag = flag && $(this).prop("checked");
		})
		$("#checkAll").prop("checked", flag);
	}
	function doChangeTBodyCheckBoxState() {
		var flag = $(this).prop("checked");
		$("#tbodyId input[type='checkbox']").prop("checked", flag);
	}

	function doGetCheckedIds() {
		var array = [];
		$("#tbodyId input[type='checkbox']").each(function() {
			if ($(this).prop("checked")) {
				//调用数组对象的push方法将选中对象的值存储到数组
				array.push($(this).val());
			}
		});
		return array;
	}

	function doDeleteObjects() {
		debugger
		var idArray = doGetCheckedIds();
	
		if (idArray.length == 0) {
			alert("请选择一条数据");
			return;
		}
		if (!confirm("确认删除吗"))
			return;

		var params = {
			"ids" : idArray.toString()
		};//1,2,3

		var url = "log/doDeleteObjects";
		$.post(url, params, function(result) {
			console.log(result)
			if (result.state == 1) {
				alert(result.message);
				//doGetObjects();
				doRefreshAfterDelete();
			} else {
				alert(result.message);
			}
		})
	}
	
	function doRefreshAfterDelete(){
		   var pageCurrent=$("#pageId").data("pageCurrent");
		   var pageCount=$("#pageId").data("pageCount");
		   var isCheckedAll=$("#checkAll").prop("checked");
		   var isLastPage=(pageCurrent==pageCount);
		   var isFirstPage = (pageCurrent==1);
		   if(isCheckedAll&&isLastPage&&!isFirstPage){
			   pageCurrent--;
			   $("#pageId").data("pageCurrent",pageCurrent);
		   }else if(isCheckedAll&&isLastPage&&isFirstPage){
			   $("#tbodyId").html("数据库已无记录");
			   $("#checkAll").prop("checked",false)
			   return;
		   }else if(!isCheckedAll&&isLastPage&&isFirstPage){
			   $("#tbodyId input[type='checkbox']")
			   .each(function(){
				   if($(this).prop("checked")){
					   $(this).parents("tr").remove();
					   rowCount--;
				   }
			   })
			   return;
			   
			   $(".rowCount").html("rowCount",rowCount);
			   $("#pageId").data("rowCount",rowCount);
		   }
		      doGetObjects();
		   
	   }
	 

	function doQueryObjects() {
		$("#pageId").data("pageCurrent", 1);
		doGetObjects();
	}
	function doGetObjects() {
		$("#checkAll").prop("checked", false)
		var url = "log/doFindPageObjects"
		var pageCurrent = $("#pageId").data("pageCurrent");
		if (!pageCurrent)
			pageCurrent = 1;
		var uname = $("#searchNameId").val();
		if (uname)
			params.username = uname;
		var params = {
			"pageCurrent" : pageCurrent
		};
		$.getJSON(url, params, function(result) {
			doHandleReponseResult(result)
		});
	}
	function doHandleReponseResult(result) {
		if (result.state == 1) {
			showRows(result.data.records);
			doSetPagination(result.data);
		} else {
			alert(result.message)
		}
	}
	
	function showRows(records) {
		var tbody = $("#tbodyId")
		tbody.empty();
		//2.迭代records记录，并将其内容追加到tbody
		for ( var i in records) {
			//2.1 构建tr对象
			var tr = $("<tr></tr>");
			//2.2 构建tds对象
			var tds = doCreateTds(records[i]);
			//2.3 将tds追加到tr中
			tr.append(tds);
			//2.4 将tr追加到tbody中
			tbody.append(tr);
		}
	}
	function doCreateTds(data) {
		var tds = "<td><input type='checkbox' class='cBox' name='cItem' value='"+data.id+"'></td>"
				+ "<td>"
				+ data.username
				+ "</td>"
				+ "<td>"
				+ data.operation
				+ "</td>"
				+ "<td>"
				+ data.method
				+ "</td>"
				+ "<td>"
				+ data.params
				+ "</td>"
				+ "<td>"
				+ data.ip
				+ "</td>"
				+ "<td>"
				+ data.time + "</td>";
		return tds;
	}
</script>






